package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationEs;
import cn.wolfcode.luowowo.search.domain.StrategyEs;
import cn.wolfcode.luowowo.search.domain.TravelEs;
import cn.wolfcode.luowowo.search.domain.UserInfoEs;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.ISearchService;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping
public class SearchController {
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISearchService searchService;
    //查询语句的接口
    @GetMapping("/q")
    //接收值用的是qo
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        //浏览器解码操作
        String decode = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        //解码后的结果
        qo.setKeyword(decode);
        //使用switch的方法匹配前端传的值
        System.out.println(qo.getKeyword());
        switch (qo.getType()){
            case SearchQueryObject.TYPE_DEST:
                return searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStratrgy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(qo);
            case SearchQueryObject.TYPE_USER:
                return searchUser(qo);
            default:return searchAll(qo);
        }
    }
    //查用户
    private Object searchUser(SearchQueryObject qo){
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "nickname", "info");
        HashMap<String, Object> map = new HashMap<>();
        map.put("qo",qo);
        map.put("page",page);
        return JsonResult.success(map);
    }
    //查攻略
    private Object searchStratrgy(SearchQueryObject qo){
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subtitle", "summary");
        HashMap<String, Object> map = new HashMap<>();
        map.put("qo",qo);
        map.put("page",page);
        return JsonResult.success(map);
    }
    //查游记
    private Object searchTravel(SearchQueryObject qo){
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");
        HashMap<String, Object> map = new HashMap<>();
        map.put("qo",qo);
        map.put("page",page);
        return JsonResult.success(map);
    }
    //查目的地
    private Object searchDest(SearchQueryObject qo){
        SearchResult result = new SearchResult();
        //判断目的地是否存在
        Destination dest = destinationService.findByDestName(qo.getKeyword());
        
        //如果存在.查询目的地下的所有攻略游记和用户
        if (dest != null){
            List<Strategy> sts = strategyService.findByDestName(dest.getName());
            result.setStrategys(sts);
            List<Travel> ts = travelService.findByDestName(dest.getName());
            result.setTravels(ts);
            List<UserInfo>us = userInfoService.findByCity(dest.getName());
            result.setUsers(us);
            result.setTotal(sts.size()+ts.size()+us.size()+0L);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("dest",dest);
        map.put("qo",qo);
        map.put("result",result);
        return JsonResult.success(map);
    }
    //查全部
    private Object searchAll(SearchQueryObject qo){
        SearchResult result = new SearchResult();
        //攻略
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subtitle", "summary");
        //游记
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title",  "summary");
        //用户
        Page<UserInfo> user = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "nickname",  "info");
        //目的地
        Page<Destination> dest = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo,
                 "info");
        result.setStrategys(sts.getContent());
        result.setTravels(ts.getContent());
        result.setUsers(user.getContent());
        result.setDests(dest.getContent());
        
        
        //分页的总记录数
       result.setTotal(sts.getTotalElements()+ts.getTotalElements()+user.getTotalElements()+dest.getTotalElements());
       
        HashMap<String, Object> map = new HashMap<>();
        map.put("qo",qo);
        map.put("result",result);
        return JsonResult.success(map);
    }
}
